package com.my.core.util.img;

import lombok.extern.slf4j.Slf4j;
import net.sourceforge.tess4j.Tesseract;
import net.sourceforge.tess4j.TesseractException;

import java.awt.image.BufferedImage;
import java.util.Objects;
import java.util.Random;

/**
 * tesseract OCR图片识别工具类
 * @author lhp
 */
@Slf4j
public class OcrUtil {

    /**
     * 识别验证码图片
     * @param dataPath tessdata 路径
     * @param image 图片
     * @return 识别结果
     */
    public static String recognizeText(String dataPath, BufferedImage image) {
        Tesseract tesseract = new Tesseract();
        // 设置训练数据路径，需根据实际情况调整
        tesseract.setDatapath(dataPath);
        // 设置识别语言为英文，可按需更改
        tesseract.setLanguage("eng");
        try {
            // 识别验证码图片，需将路径替换为实际验证码图片路径
            String result = tesseract.doOCR(image);
            log.info("ocr识别图片验证码结果: {}", result.trim());
            return result.trim();
        } catch (TesseractException e) {
            log.error("tesseract库OCR图片识别异常{}", e.getMessage(), e);
        }
        return null;
    }

    /**
     * ocr识别结果计算 计算表达式
     * @param expression 表达式
     * @return 计算结果
     */
    public static String calculation(String expression){
        //判断字符串是否存在英文
        if (expression.matches(".*[a-zA-Z]+.*")) {
            if (expression.contains(")")){
                expression = expression.replaceAll("\\)", "J");
            }
            return expression;
        }

        // 去除字符串中的非数字和非运算符字符
        expression = expression.replaceAll("[^0-9-+*/=]", "");
        //只需要字符串的前3位
        expression = expression.substring(0, 3);

        //7+3 识别成743 需要处理下
        if (!expression.matches("[-+*/]") && expression.contains("4")) {
            char[] charArray = expression.toCharArray();
            char newChar = '+';
            charArray[1] = newChar;
            expression = new String(charArray);
        }

        String[] operandsAndOperator = expression.split("[-+*/]");
        int num1 = Integer.parseInt(operandsAndOperator[0]);
        int num2 = Integer.parseInt(operandsAndOperator[1]);
        char operator = expression.replaceAll("[0-9]", "").charAt(0);
        int result = 0;
        switch (operator) {
            case '-':
                result = num1 - num2;
                break;
            case '+':
                result = num1 + num2;
                break;
            case '*':
                result = num1 * num2;
                break;
            case '/':
                if (num2 != 0) {
                    result = num1 / num2;
                } else {
                    log.warn("除数不能为0");
                }
                break;
        }
        //System.out.println(expression + " 结果是：" + result);
        return Objects.toString(result);

    }

    /**
     * 生成13位随机数字字符串
     * @return  13位随机数字字符串
     */
    public static String generateRandom13Digits() {
        Random random = new Random();
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < 13; i++) {
            sb.append(random.nextInt(10));
        }
        return sb.toString();
    }


}
